package com.jms.framework.context;

import com.jms.framework.aop.context.AopContext;
import com.jms.framework.beans.context.BeanContext;
import com.jms.framework.config.JmsConfig;
import com.jms.framework.jdbc.context.JDBCContext;
import com.jms.framework.listener.context.ListenerContext;
import com.jms.framework.mvc.context.MvcContext;

public abstract class AbstractApplicationContext implements ApplicationContext {
    protected JmsConfig jmsConfig;
    protected ListenerContext listenerContext;
    protected BeanContext beanContext;
    protected JDBCContext jdbcContext;
    protected MvcContext mvcContext;
    protected AopContext aopContext;
    @Override
    public JmsConfig getJmsConfig() {
        return jmsConfig;
    }

    @Override
    public ListenerContext getListenerContext() {
        return listenerContext;
    }

    @Override
    public Object getBean(String beanName) {
        return beanContext.getBean(beanName);
    }

    @Override
    public <T> T getBean(Class<T> beanClass) {
        return beanContext.getBean(beanClass);
    }

    @Override
    public Object[] getBeans() {
        return beanContext.getBeans();
    }


    @Override
    public void init() {
        if (jmsConfig != null) {
            jmsConfig.initConfig();
        }
        if (aopContext != null) {
            aopContext.init();
        }
        if (beanContext != null) {
            beanContext.init();
        }
        if (jdbcContext != null) {
            jdbcContext.init();
        }
        if (mvcContext != null) {
            mvcContext.init();
        }
        if (listenerContext != null) {
            listenerContext.init();
        }

    }

    @Override
    public void destroy() {
        if (listenerContext != null) {
            listenerContext.getCloseListenerChain().doChain();
        }
        if (aopContext != null) {
            aopContext.destroy();
        }
        if (beanContext != null) {
            beanContext.destroy();
        }
        if (jdbcContext != null) {
            jdbcContext.destroy();
        }
        if (mvcContext != null) {
            mvcContext.destroy();
        }
        if (listenerContext != null) {
            listenerContext.destroy();
        }
        if (jmsConfig != null) {
            jmsConfig.destroy();
        }
    }

    public void setListenerContext(ListenerContext listenerContext) {
        this.listenerContext = listenerContext;
    }

    @Override
    public BeanContext getBeanContext() {
        return beanContext;
    }

    @Override
    public JDBCContext getJDBCContext() {
        return jdbcContext;
    }

    @Override
    public MvcContext getMvcContext() {
        return mvcContext;
    }

    public void setBeanContext(BeanContext beanContext) {
        this.beanContext = beanContext;
    }

    public void setJdbcContext(JDBCContext jdbcContext) {
        this.jdbcContext = jdbcContext;
    }

    public void setMvcContext(MvcContext mvcContext) {
        this.mvcContext = mvcContext;
    }

    @Override
    public AopContext getAopContext() {
        return aopContext;
    }

    public void setAopContext(AopContext aopContext) {
        this.aopContext = aopContext;
    }
}
